package com.fjc.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fjc.domain.RequestData;
import com.fjc.domain.ResultObject;
import com.fjc.security.AESUtil;
import com.fjc.security.AnalysisRequestData;
import com.fjc.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by fjc on 2018/11/20.
 */
@RestController
@RequestMapping("user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 登陆
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("login")
    public ResultObject login(RequestData data)throws Exception{
        log.info("接收到原始数据:" + data);
        String deKey = AnalysisRequestData.decryptKey(data.getKey());
        JSONObject jsonObject = AnalysisRequestData.decryptData(data.getData());
        log.info("RSA解密后key:" + deKey + ",RSA解密后data:" + jsonObject);
        String mobile = AESUtil.aesDecrypt(jsonObject.getString("mobile"),deKey);
        String pwd = AESUtil.aesDecrypt(jsonObject.getString("pwd"),deKey);
        log.info("AES解密后mobile:" + mobile + ",AES解密后pwd:" + pwd);
        return userService.login(mobile,pwd);

    }

    /**
     * 获取短信
     * @param data
     * @return
     */
    @RequestMapping("getMsg")
    public ResultObject getMsg(RequestData data)throws Exception{
        log.info("接收到原始数据:" + data);
        String deKey = AnalysisRequestData.decryptKey(data.getKey());
        JSONObject jsonObject = AnalysisRequestData.decryptData(data.getData());
        log.info("RSA解密后key:" + deKey + ",RSA解密后data:" + jsonObject);
        String mobile = AESUtil.aesDecrypt(jsonObject.getString("mobile"),deKey);
        log.info("AES解密后mobile:" + mobile );
        return userService.getMsg(mobile);
    }

}
